//
//  CItemPreView.m
//  HCHK_iphone
//
//  Created by yilong zhang on 11-12-7.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CItemPreView.h"

#define SET_FRAME(ARTICLEX) x = ARTICLEX.frame.origin.x + increase;\
if(x < 0) x = pageWidth * 2;\
if(x > pageWidth * 2) x = 0.0f;\
[ARTICLEX setFrame:CGRectMake(x, \
ARTICLEX.frame.origin.y,\
ARTICLEX.frame.size.width,\
ARTICLEX.frame.size.height)]

//================================================================================================================================

@implementation CPreItem

@synthesize m_RootPanel;
@synthesize m_PdctName;
@synthesize m_ResName;
@synthesize m_ResAddr;
@synthesize m_Local;
@synthesize m_Distance;
@synthesize m_MoreComsBtn;
@synthesize m_PubNamelb;
@synthesize m_PdtSum;
@synthesize m_DelBtn;
@synthesize m_NiceBtn;
@synthesize m_WantBtn;
@synthesize m_Image;
@synthesize m_ImageBtn;
@synthesize m_Drawer;
@synthesize m_DrawerBtn;
@synthesize m_ShareBtn;
@synthesize m_ChangeBtn;
@synthesize delegate;
@synthesize m_Product;
@synthesize m_bIsFavorite;
@synthesize m_bIsImageNull;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        m_Product = nil;
        m_bIsFavorite = NO;
        m_bIsImageNull= YES;
        self.backgroundColor = [UIColor clearColor];
        
        //rootpanel
        /*UIView *rootback = [[UIView alloc] initWithFrame:CGRectMake(5, 10, PANEL_WIDTH, frame.size.height)];
        rootback.backgroundColor = [UIColor whiteColor];
        rootback.layer.shadowOffset = CGSizeMake(2, 2);
        rootback.layer.shadowColor  = [[UIColor blackColor] CGColor];
        rootback.layer.shadowOpacity= 0.5;
        [self addSubview:rootback];
        [rootback release];*/
        
        //rootpanel
        m_RootPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, PANEL_WIDTH, PANEL_HEIGHT)];
        /*m_RootPanel.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:0.964 green:0.968 blue:0.949 alpha:1.0];
        m_RootPanel.layer.cornerRadius = 6;
        m_RootPanel.layer.shadowOpacity= 0.8;
        m_RootPanel.layer.shadowColor  = [[UIColor blackColor] CGColor];
        m_RootPanel.layer.shadowOffset = CGSizeMake(3, 3);*/
        //m_RootPanel.clipsToBounds = YES;
        [self addSubview:m_RootPanel];
        [m_RootPanel release];
        
        UIImageView *panelback = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, PANEL_WIDTH, PANEL_HEIGHT)];
        panelback.image = [UIImage imageNamed:@"preitembg.png"];
        [m_RootPanel addSubview:panelback];
        [panelback release];
        
        //more commentries
        /*m_MoreComsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_MoreComsBtn.tag = 2;//3;
        m_MoreComsBtn.frame = CGRectMake(m_RootPanel.frame.size.width-72, 4, 75, 55);
        [m_MoreComsBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_MoreComsBtn setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
        [m_RootPanel addSubview:m_MoreComsBtn];*/
        
        //resaddr
        /*m_ResAddr = [[UILabel alloc] initWithFrame:CGRectMake(9, m_PdctName.frame.origin.y+m_MoreComsBtn.frame.size.height-15, m_PdctName.frame.size.width, 15)];
        m_ResAddr.font = [UIFont systemFontOfSize:10];
        m_ResAddr.backgroundColor = [UIColor clearColor];
        m_ResAddr.textColor = [UIColor colorWithRed:0.333 green:0.333 blue:0.333 alpha:1.0];
        [m_RootPanel addSubview:m_ResAddr];
        [m_ResAddr release];*/
        
        //resname
        /*m_ResName = [[UILabel alloc] initWithFrame:CGRectMake(9, m_ResAddr.frame.origin.y-15, m_PdctName.frame.size.width-60, 15)];
        m_ResName.font = [UIFont systemFontOfSize:10];
        m_ResName.backgroundColor = [UIColor clearColor];
        m_ResName.textColor = [UIColor colorWithRed:0.333 green:0.333 blue:0.333 alpha:1.0];
        [m_RootPanel addSubview:m_ResName];
        [m_ResName release];*/
        
        //local icon
        /*m_Local = [[UIImageView alloc] initWithFrame:CGRectMake(m_ResAddr.frame.origin.x+m_ResAddr.frame.size.width+5, m_ResAddr.frame.origin.y, 8, 12)];
        m_Local.image = [UIImage imageNamed:@"pin.png"];
        [m_RootPanel addSubview:m_Local];
        [m_Local release];*/
        
        //distance
        /*m_Distance = [[UILabel alloc] initWithFrame:CGRectMake(m_Local.frame.origin.x+m_Local.frame.size.width, m_ResAddr.frame.origin.y, 55, 15)];
        m_Distance.font = [UIFont italicSystemFontOfSize:12];
        m_Distance.backgroundColor = [UIColor clearColor];
        m_Distance.textAlignment = UITextAlignmentLeft;
        m_Distance.textColor = [UIColor colorWithRed:0.333 green:0.333 blue:0.333 alpha:1.0];
        [m_RootPanel addSubview:m_Distance];
        [m_Distance release];*/
        
        //image
        m_Image = [[UIImageView alloc] initWithFrame:CGRectMake(9, 8, IMAGE_WIDTH, IMAGE_HEIGHT)];
        m_Image.backgroundColor = [UIColor clearColor];
        m_Image.layer.masksToBounds= YES;
        m_Image.layer.cornerRadius = 5;
        [m_RootPanel addSubview:m_Image];
        [m_Image release];
        
        //imagebutton
        m_ImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ImageBtn.frame = CGRectMake(m_Image.frame.origin.x, m_Image.frame.origin.y, m_Image.frame.size.width, m_Image.frame.size.height);
        m_ImageBtn.alpha = 0.1;
        m_ImageBtn.tag = 3;
        [m_ImageBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_RootPanel addSubview:m_ImageBtn];
        
        //name
        m_PdctName = [[UILabel alloc] initWithFrame:CGRectMake(10, m_RootPanel.frame.size.height-40, m_RootPanel.frame.size.width-25, 25)];
        //m_PdctName.font = [UIFont boldSystemFontOfSize:20];
        //m_PdctName.font = [UIFont systemFontOfSize:20];
        //UIFont *font = [UIFont fontWithName:@"迷你简准圆" size:20];
        m_PdctName.font = [UIFont fontWithName:@"迷你简准圆" size:20];
        //m_PdctName.font = [UIFont fontWithName:@"简准圆" size:20];
        m_PdctName.textColor = [UIColor colorWithRed:0.407 green:0.152 blue:0.003 alpha:1.0];
        m_PdctName.textAlignment = UITextAlignmentRight;
        m_PdctName.backgroundColor = [UIColor clearColor];
        [m_RootPanel addSubview:m_PdctName];
        [m_PdctName release];
        
        UIImageView *part = [[UIImageView alloc] initWithFrame:CGRectMake(-2, -9, 53, 53)];
        part.image = [UIImage imageNamed:@"part.png"];
        [m_RootPanel addSubview:part];
        [part release];
        
        //favorite
        /*m_DrawerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_DrawerBtn.frame = CGRectMake(m_Image.frame.origin.x+10, m_Image.frame.origin.y+10, 31, 31);
        m_DrawerBtn.tag = 0;
        [m_DrawerBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_DrawerBtn setBackgroundImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
        m_DrawerBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [m_RootPanel addSubview:m_DrawerBtn];*/
        
        //sharebtn
        /*m_ShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ShareBtn.frame = CGRectMake(m_DrawerBtn.frame.origin.x, m_DrawerBtn.frame.origin.y+m_DrawerBtn.frame.size.height+15, 31, 31);
        m_ShareBtn.tag = 1;
        [m_ShareBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_ShareBtn setBackgroundImage:[UIImage imageNamed:@"share.png"] forState:UIControlStateNormal];
        [m_RootPanel addSubview:m_ShareBtn];*/
        
        //Drawer
        /*UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(m_RootPanel.frame.origin.x-4, 211, 5, 47)];
        imageview.image = [UIImage imageNamed:@"1.png"];
        [self addSubview:imageview];
        [imageview release];
        
        m_Drawer = [[UIView alloc] initWithFrame:CGRectMake(45-148, 203, 148, 44)];
        [m_RootPanel addSubview:m_Drawer];
        [m_Drawer release];

        //bakcimage
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, m_Drawer.frame.size.width, 44)];
        imageview.image = [UIImage imageNamed:@"e.png"];
        imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
        [m_Drawer addSubview:imageview];
        [imageview release];
        
        //changebtn
        m_ChangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_ChangeBtn.frame = CGRectMake(59, 2, 38, 38);
        m_ChangeBtn.tag = 2;
        [m_ChangeBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_ChangeBtn setBackgroundImage:[UIImage imageNamed:@"location.png"] forState:UIControlStateNormal];
        [m_Drawer addSubview:m_ChangeBtn];
        
        //drawer button
        m_DrawerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_DrawerBtn.frame = CGRectMake(m_Drawer.frame.size.width-35, 5, 31, 31);
        m_DrawerBtn.tag = 0;
        [m_DrawerBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_DrawerBtn setBackgroundImage:[UIImage imageNamed:@"4.png"] forState:UIControlStateNormal];
        m_DrawerBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        [m_Drawer addSubview:m_DrawerBtn];*/
        
        /*publisher's name
        m_PubNamelb = [[UILabel alloc] initWithFrame:CGRectMake(m_Image.frame.origin.x+m_Image.frame.size.width+9, m_Image.frame.origin.y+5, m_RootPanel.frame.size.width-m_Image.frame.size.width-27, 13)];
        m_PubNamelb.font = [UIFont boldSystemFontOfSize:11];
        m_PubNamelb.backgroundColor = [UIColor clearColor];
        m_PubNamelb.textColor = [UIColor colorWithRed:0.447 green:0.501 blue:0.137 alpha:1.0];
        [m_RootPanel addSubview:m_PubNamelb];
        [m_PubNamelb release];
        
        //summary of product
        m_PdtSum = [[UILabel alloc] initWithFrame:CGRectMake(m_PubNamelb.frame.origin.x, m_PubNamelb.frame.origin.y+m_PubNamelb.frame.size.height, m_PubNamelb.frame.size.width, 12)];
        m_PdtSum.font = [UIFont systemFontOfSize:10];
        m_PdtSum.backgroundColor = [UIColor clearColor];
        [m_RootPanel addSubview:m_PdtSum];
        [m_PdtSum release];
        
        //m_WantBtn
        m_WantBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_WantBtn.frame = CGRectMake(m_PubNamelb.frame.origin.x+2, m_RootPanel.frame.size.height-35-9, m_PdtSum.frame.size.width-4, 35);
        m_WantBtn.tag = 6;
        m_WantBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [m_WantBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [m_WantBtn setTitle:@"想吃  0" forState:UIControlStateNormal];
        [m_WantBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_WantBtn setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal];
        [m_RootPanel addSubview:m_WantBtn];
        
        //nicebtn
        m_NiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_NiceBtn.frame = CGRectMake(m_PubNamelb.frame.origin.x+2, m_WantBtn.frame.origin.y-35-2, m_PdtSum.frame.size.width-4, 35);
        m_NiceBtn.tag = 5;
        m_NiceBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [m_NiceBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [m_NiceBtn setTitle:@"好看  0" forState:UIControlStateNormal];
        [m_NiceBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_NiceBtn setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal];
        [m_RootPanel addSubview:m_NiceBtn];
        
        //delbtn
        m_DelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        m_DelBtn.frame = CGRectMake(m_PubNamelb.frame.origin.x+2, m_NiceBtn.frame.origin.y-35-2, m_PdtSum.frame.size.width-4, 35);
        m_DelBtn.tag = 4;
        m_DelBtn.titleLabel.font = [UIFont systemFontOfSize:10];
        [m_DelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [m_DelBtn setTitle:@"好吃  0" forState:UIControlStateNormal];
        [m_DelBtn addTarget:self action:@selector(OnButtonDown:) forControlEvents:UIControlEventTouchUpInside];
        [m_DelBtn setBackgroundImage:[UIImage imageNamed:@"6.png"] forState:UIControlStateNormal];
        [m_RootPanel addSubview:m_DelBtn];*/
    }
    
    return self;
}

-(void)dealloc
{
    //NSLog(@"\r\n\r\nItemPrew_dealloc\r\n\r\n");
    [super dealloc];
}

-(void)OnButtonDown:(UIButton*)sender
{
    //0-drawer
    if (sender.tag < 3)
    {
        if (m_Drawer.center.x < 0)
        {
            [self OpenDrawer];
        }
        else 
        {
            //[self OpenDrawer];
            //[self performSelector:@selector(DidDrawerOpened:) withObject:[NSNumber numberWithInt:sender.tag] afterDelay:0.5];
            [self DidDrawerOpened:[NSNumber numberWithInt:sender.tag]];
        }
    }
    else
    {
        [delegate DidPreItemBtnClicked:sender.tag Object:self];
    }
}

-(void)DidDrawerOpened:(id)object
{
    [delegate DidPreItemBtnClicked:[object intValue] Object:self];
    return;
    
    if (2 == [object intValue] && m_bIsFavorite)
        return;
    else 
        [delegate DidPreItemBtnClicked:[object intValue] Object:self];
}

-(void)OpenDrawer
{
    if (m_Drawer.center.x < 0)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        m_Drawer.center = CGPointMake(m_Drawer.center.x+(m_Drawer.frame.size.width-45), m_Drawer.center.y);
        [UIView commitAnimations];
    }
    else 
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        m_Drawer.center = CGPointMake(45-m_Drawer.frame.size.width/2, m_Drawer.center.y);
        [UIView commitAnimations];
    }
}

-(void)SetInfo:(CProduct*)product ID:(int)id
{
    m_Product = product;
    
    if (m_Product)
    {
        m_Product.delegate = self;
        
        m_PdctName.text = product.P_Name;
        m_ResName.text  = product.P_ResName;
        m_PubNamelb.text= product.P_PubName;
        
        //计算地址显示宽度
        m_ResAddr.text  = product.P_Position;
        CGSize size = [m_ResAddr.text sizeWithFont:m_ResAddr.font constrainedToSize:CGSizeMake(MAXFLOAT, m_ResAddr.frame.size.height)];
        CGFloat fwidth = size.width;
        fwidth = (fwidth > m_ResName.frame.size.width) ? (m_ResName.frame.size.width) : (fwidth);
        m_ResAddr.frame = CGRectMake(m_ResAddr.frame.origin.x, m_ResAddr.frame.origin.y, fwidth, m_ResAddr.frame.size.height);
        
        /*计算美食概述显示高度
        CGFloat fheight = [CDisContent GetDicContentHeight:product.P_Caption Width:m_PdtSum.frame.size.width Font:m_PdtSum.font]-25;
        NSString *str = @"W";
        CGSize size1= [str sizeWithFont:m_PdtSum.font constrainedToSize:CGSizeMake(m_PdtSum.frame.size.width, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
        m_PdtSum.numberOfLines = fheight/size1.height;
        m_PdtSum.text = product.P_Caption;
        fheight = (fheight > 38) ? (38) : (fheight);
        m_PdtSum.frame= CGRectMake(m_PdtSum.frame.origin.x, m_PdtSum.frame.origin.y, m_PdtSum.frame.size.width, fheight);

        //设置好吃，好看，想吃
        [m_DelBtn setTitle:[NSString stringWithFormat:@"好吃  %ld", [m_Product.P_Eat longValue]] forState:UIControlStateNormal];
        [m_NiceBtn setTitle:[NSString stringWithFormat:@"好看  %ld", [m_Product.P_Nice longValue]] forState:UIControlStateNormal];
        [m_WantBtn setTitle:[NSString stringWithFormat:@"想吃  %ld", [m_Product.P_Want longValue]] forState:UIControlStateNormal];*/
        
        //NSLog(@"当前浏览美食经信息__(ID:%ld),(latitude:%f, longtitude:%f),(名称:%@),(餐馆:%@),(地址:%@),(发布者:%@)\n\r\n\r", [product.P_AID longValue], [product.P_WDu floatValue], [product.P_JDu floatValue], product.P_Name, product.P_ResName, product.P_Position, product.P_PubName);
        
        //设置距离
        NSString *message = nil;
        if (NormalSupport == [[CLocator DefaultLocator] CheckDevLocation])
        {
            if ((0.000000 != [product.P_WDu floatValue]) && (0.000000 != [product.P_JDu floatValue]))
            {
                double dis = [self GetLocationDistance:[CUser DefaultUser].m_SelfPosition New:CLLocationCoordinate2DMake([product.P_WDu floatValue], [product.P_JDu floatValue])]/1000;
                if (dis < 1)
                {
                    dis *= 1000;
                    dis += 1;
                    message = [NSString stringWithFormat:@"%0.0fm", dis];
                }
                else
                {
                    message = [NSString stringWithFormat:@"%0.0fkm", dis];
                }
            }
            else 
            {
                message = [NSString stringWithFormat:@"%0.0fm", 1.00000];
            }
        }
        else 
        {
            //NSLog(@"定位未代开，与美食间的距离为1m。\r\n\r\n");
            message = [NSString stringWithFormat:@"%0.0fm", 1.00000];
        }
        m_Local.frame = CGRectMake(m_ResAddr.frame.origin.x+m_ResAddr.frame.size.width, m_Local.frame.origin.y, m_Local.frame.size.width, m_Local.frame.size.height); 
        m_Distance.frame= CGRectMake(m_Local.frame.origin.x+m_Local.frame.size.width, m_Distance.frame.origin.y, m_Distance.frame.size.width, m_Distance.frame.size.height);
        m_Distance.text = message;
    }
    else 
    {
        m_Product.delegate = nil;
        m_PdctName.text    = nil;
        m_ResName.text     = nil;
        m_PubNamelb.text   = nil;
        m_ResAddr.text     = nil;
        m_Distance.text    = nil;
        m_Image.image      = nil;
        m_PdtSum.text      = nil;
    }
    
    self.tag = id;
}

-(void)LoadImage
{
    if (m_Product)
    {
        if (m_bIsImageNull)
        {
            NSData *imdata = [m_Product GetProductImage];
            m_Product.m_ImageView = m_Image;
            if (imdata)
            {
                m_Image.image = [UIImage imageWithData:imdata];
            }
            else 
            {
                //m_Image.image = [UIImage imageWithData:[CMainPageCtrl DefaultMainPageCtrl].m_DfData];
                [m_Product DownLoadImage];
            }
            
            m_Image.alpha = 0.0;
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.3];
            m_bIsImageNull = NO;
            m_Image.alpha = 1.0;
            [UIView commitAnimations];
        }
    }
}

-(void)DidDownLoadSubImage:(id)product Image:(NSData *)image Object:(id)object
{
    return;
}

-(void)DidDownLoadImage:(id)product Image:(NSData*)image
{
    if (m_Product == product)
    {
        //m_Image.image = [UIImage imageWithData:image];
        [m_Image SetImageWithAnimation:[UIImage imageWithData:image] Animation:YES];
    }
}

-(void)DidDownLoadCommentaries:(id)product Commetaries:(NSArray *)coms
{
    ;
}

-(void)DidDownLoadSuccess:(id)product OpType:(int)optype Index:(int)index Info:(NSString*)info
{
    CProduct *prdct  = product;
    
    //commitpingjia
    if (3 == optype)
    {
        if (m_Product == prdct)
        {
            CTabView *tabview = [CMainPageCtrl DefaultMainPageCtrl].m_TabView;
            NSString *tablename = nil;
            if (0 == tabview.m_Tab.m_nFocus)
            {
                tablename = [NSString stringWithString:@"Best"];
            }
            else if (1 == tabview.m_Tab.m_nFocus)
            {
                tablename = [NSString stringWithString:@"Nearest"];
            }
            else if (2 == tabview.m_Tab.m_nFocus)
            {
                tablename = [NSString stringWithString:@"Newest"];
            }
            else {;}
            
            CKey *key =[[CKey alloc] initWithType:1 Value:prdct.P_Eat Name:@"P_Eat"];
            [CProduct UpdateProductInfo:tablename Where:[NSString stringWithFormat:@"P_AID=%lld", [prdct.P_AID longLongValue]] Keys:[NSArray arrayWithObjects:key, nil]];
            [key release];
            
            //del
            if (0 == index)
            {
                [m_DelBtn setTitle:[NSString stringWithFormat:@"好吃  %ld", [m_Product.P_Eat longValue]] forState:UIControlStateNormal];
            }
            //want
            else if (1 == index)
            {
                [m_WantBtn setTitle:[NSString stringWithFormat:@"想吃  %ld", [m_Product.P_Want longValue]] forState:UIControlStateNormal];
            }
            //nice
            else if (2 == index)
            {
                [m_NiceBtn setTitle:[NSString stringWithFormat:@"好看  %ld", [m_Product.P_Nice longValue]] forState:UIControlStateNormal];
            }
            else {;}
        }
    }
}

-(void)DidDownLoadError:(id)product OpType:(int)optype
{
    //
}

-(double)GetLocationDistance:(CLLocationCoordinate2D)cur New:(CLLocationCoordinate2D)new
{
    CLLocation *l1 = [[[CLLocation alloc] initWithLatitude:new.latitude longitude:new.longitude] autorelease];
    CLLocation *l2 = [[[CLLocation alloc] initWithLatitude:cur.latitude longitude:cur.longitude] autorelease];
    
    return [l2 distanceFromLocation:l1];
}

@end

//================================================================================================================================

@implementation UIScrollViewEx

-(void)dealloc
{
    NSLog(@"\r\n\r\nUIScrollViewEx_dealloc\r\n\rn");
    
    [super dealloc];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.scrollEnabled = YES;
    //NSLog(@"ScrollEx_Began");
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"ScrollEx_Ended");
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //NSLog(@"ScrollEx_Moved");
}

@end

//================================================================================================================================

@implementation CItemPreView

@synthesize m_delegate;

@synthesize m_RootPanel;
@synthesize m_Direction;
@synthesize m_Loading;
@synthesize m_Offset;
@synthesize m_nCurIndex;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:view];
        [view release];
        
        [self InitItemPreView];
    }
    return self;
}

-(void)InitItemPreView
{
    m_nCurIndex = 0;
    m_RootPanel = [[UIScrollView alloc] initWithFrame:CGRectMake(12, 0, PAGE_WIDTH, PAGE_HEIGHT)];
    //m_RootPanel.center = CGPointMake(self.frame.size.width/2, m_RootPanel.center.y);
    m_RootPanel.delegate = self;
    m_RootPanel.contentSize = CGSizeMake(0, m_RootPanel.frame.size.height);
    m_RootPanel.pagingEnabled = YES;
    m_RootPanel.autoresizesSubviews = YES;
    m_RootPanel.clipsToBounds = NO;
    m_RootPanel.showsHorizontalScrollIndicator = NO;
    m_RootPanel.showsVerticalScrollIndicator   = NO;
    [self addSubview:m_RootPanel];
    [m_RootPanel release];
}

-(void)ReSetItemPreView
{
    [self HiddenLoading];
    [m_RootPanel removeFromSuperview];
    [self InitItemPreView];
}

-(void)ShowLoading
{
    if (nil == m_Loading)
    {
        m_Loading = [[CPreItem alloc] initWithFrame:CGRectMake(m_RootPanel.contentSize.width, 0, PAGE_WIDTH, PAGE_HEIGHT)];
        [m_RootPanel addSubview:m_Loading];
        [m_Loading release];
    }
    else 
    {
        m_Loading.frame = CGRectMake(m_RootPanel.contentSize.width, 0, PAGE_WIDTH, PAGE_HEIGHT);
    }
}

-(void)HiddenLoading
{
    [m_Loading removeFromSuperview];
    m_Loading = nil;
}

-(void)SetItems:(NSArray*)items
{
    int nIndex = [[m_RootPanel subviews] count];
    CPreItem *item = nil;
    
    [self HiddenLoading];
    
    for (CProduct *product in items)
    {
        item = [[CPreItem alloc] initWithFrame:CGRectMake(m_RootPanel.contentSize.width, 0, PAGE_WIDTH, PAGE_HEIGHT)];
        item.delegate = self;
        item.tag = nIndex++;
        [item SetInfo:product ID:0];
        [m_RootPanel setContentSize:CGSizeMake(m_RootPanel.contentSize.width+item.frame.size.width, m_RootPanel.contentSize.height)];
        [m_RootPanel addSubview:item];
        [item release];
    }
}

-(CPreItem*)LoadImages:(int)index
{    
    NSArray *sub = [self.m_RootPanel subviews];
    CPreItem*item= [sub objectAtIndex:index];
    [item LoadImage];

    //释放图片
    //<-left
    if ((index - 2) >= 0)
    {
        item = [sub objectAtIndex:index-2];
        item.m_Image.image = nil;
        item.m_bIsImageNull= YES;
    }
    //right->
    if ((index + 2) <= ([sub count]-1))
    {
        item = [sub objectAtIndex:index+2];
        item.m_Image.image = nil;
        item.m_bIsImageNull= YES;
    }
    
    //加载图片
    //<-left
    if ((index - 1) >= 0)
    {
        item = [sub objectAtIndex:index-1];
        [item LoadImage];
    }
    //->right
    if ((index + 1) <= ([sub count]-1))
    {
        item = [sub objectAtIndex:index+1];
        [item LoadImage];
    }
    
    return item;
}

-(void)DidPreItemBtnClicked:(int)tag Object:(CPreItem*)object
{
    [m_delegate PreViewDidSelected:tag Object:object];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint point = scrollView.contentOffset;
    
    //more
    if ((point.x+scrollView.frame.size.width) - scrollView.contentSize.width > 50)
    {
        [m_delegate DidPreViewScrollEnd:self];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    m_nCurIndex = scrollView.contentOffset.x/scrollView.frame.size.width;//PAGE_WIDTH;
    CPreItem *item = [self LoadImages:m_nCurIndex];
    [m_delegate PreViewDidEndDecelerating:m_nCurIndex Directory:0 Object:item];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    //释放图片
    NSArray *sub = [self.m_RootPanel subviews];
    CPreItem*item= [sub objectAtIndex:m_nCurIndex];
    item.m_Image.image = nil;
    //<-left
    if ((m_nCurIndex - 2) >= 0)
    {
        item = [sub objectAtIndex:m_nCurIndex-2];
        item.m_Image.image = nil;
    }
    //right->
    if ((m_nCurIndex + 2) <= ([sub count]-1))
    {
        item = [sub objectAtIndex:m_nCurIndex+2];
        item.m_Image.image = nil;
    }
    
    m_nCurIndex = scrollView.contentOffset.x/scrollView.frame.size.width;//PAGE_WIDTH;
    item = [self LoadImages:m_nCurIndex];
    [m_delegate PreViewDidEndDecelerating:m_nCurIndex Directory:0 Object:item];
}

@end

@implementation UIImageView (SetImage)

- (void)SetImageWithAnimation:(UIImage *)image Animation:(BOOL)animation
{
    if (animation)
    {
        self.alpha = 0.0;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        self.image = image;
        self.alpha = 1.0;
        [UIView commitAnimations];
    }
    else 
    {
        self.image = image;
    }
}

@end
